package mn.idax.exchange.otc.util;

import org.springframework.http.MediaType;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;

import java.io.IOException;

/**
 * @Auther: dison
 * @Date: 2018/8/28 17:55
 * @Description:
 */
public class JunitUtil {

    /**
     *
     * 功能描述:
     *      校验结果集
     * @param:
     * @return:
     * @auther: dison
     * @date: 2018/8/28 17:42
     */
    public static void verifyCommonResult(ResultActions resultActions) throws Exception{
        resultActions.andExpect(MockMvcResultMatchers.status().isOk())  // 响应码判断
                .andExpect(MockMvcResultMatchers.jsonPath("success").exists()) // 返回值判断
                .andExpect(MockMvcResultMatchers.jsonPath("success").value(0)); // 判断返回值是否正确
    }

    /**
     *
     * 功能描述:
     *      post方式请求
     * @param:
     * @return:
     * @auther: dison
     * @date: 2018/8/28 18:00
     */
    public static MockHttpServletRequestBuilder post(String uri,String token,String requestJson){
        return MockMvcRequestBuilders.post(uri)
                .header("token",token)
                .contentType(MediaType.APPLICATION_JSON_UTF8)
                .content(requestJson);
    }

    public static MockHttpServletRequestBuilder post(String uri,String token){
        return MockMvcRequestBuilders.post(uri)
                .header("token",token)
                .contentType(MediaType.APPLICATION_JSON_UTF8);
    }

    /**
     *
     * 功能描述:
     *      上传附件
     * @param:
     * @return:
     * @auther: dison
     * @date: 2018/8/28 18:45
     */
    public static MockHttpServletRequestBuilder postFile(String uri, String token, MockMultipartFile file) throws IOException {
        return MockMvcRequestBuilders.fileUpload(uri)
                .file(file)
                .header("token",token);
    }
}
